package com.smart.wechat.login.controller;

import com.smart.wechat.login.common.R;
import com.smart.wechat.login.service.IWechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 微信登录授权接口
 */
@Controller
@RequestMapping("/wechat/login")
public class WechatLoginController {

    @Autowired
    private IWechatUserService wechatUserService;

    /**
     * 微信授权，后端重定向url
     * 指向回调函数 callback()
     * @return
     */
    @GetMapping("/oauth")
    public String oauth() {
        return wechatUserService.oauth();
    }

    /**
     * 回调
     */
    @ResponseBody
    @GetMapping("/callback")
    public R<?> callback(ModelMap map, String code) throws IOException {
        return wechatUserService.callback(code);
    }

    /**
     * 微信授权，前端请求url
     * @return
     */
    @ResponseBody
    @GetMapping("/getOauthUrl")
    public R<?> getOauthUrl() {
        return wechatUserService.getOauthUrl();
    }

    /**
     * 通过code获取openId
     */
    @ResponseBody
    @GetMapping("/getOpenIdByCode")
    public R<?> getOpenIdByCode(ModelMap map, String code) throws IOException {
        return wechatUserService.getOpenIdByCode(code);
    }


}
